package cn.edu.gcu.lost.sys.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gcu.lost.sys.entity.Role;
import cn.edu.gcu.lost.sys.service.RoleService;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("findList")
    @RequiresPermissions("sys:role")
    public Object findList() {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("roleList", roleService.findList());
        resultMap.put("result", "success");
        resultMap.put("data", dataMap);
        return resultMap;
    }

    @SuppressWarnings("finally")
    @ResponseBody
    @RequestMapping("save")
    @RequiresPermissions("sys:role")
    public Object save(@RequestBody Role role) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (roleService.save(role)) {
                resultMap.put("result", "success");
                resultMap.put("data", null);
            } else {
                resultMap.put("result", "faild");
                resultMap.put("cause", "save process error");
            }
        } catch (RuntimeSqlException e) {
            e.printStackTrace();
            resultMap.put("result", "faild");
            resultMap.put("cause", "save process error");
        } finally {
            return resultMap;
        }
    }

    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions("sys:role")
    public Object delete(Role role) {
        Map<String, Object> resultMap = new HashMap<>();
        if (roleService.delete(role)) {
            resultMap.put("result", "success");
            resultMap.put("data", null);
        } else {
            resultMap.put("result", "faild");
            resultMap.put("cause", "delete process error");
        }
        return resultMap;
    }

    /*
     * isAble = 1,表示该角色可用
     * isAble = 0,表示该角色不可用
     */
    @ResponseBody
    @RequestMapping("isUseable")
    @RequiresPermissions("sys:role")
    public Object isUseable(Role role) {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isBlank(role.getId()) || role.getUseable() == null || (!role.getUseable().trim().equals("0")
                && !role.getUseable().trim().equals("1"))) {
            resultMap.put("result", "faild");
            resultMap.put("cause", "param error");
            return resultMap;
        }
        if (roleService.isUseable(role)) {
            resultMap.put("result", "success");
            resultMap.put("data", null);
        } else {
            resultMap.put("result", "faild");
            resultMap.put("cause", "update useable error");
        }
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("findDetail")
    @RequiresPermissions("sys:role")
    public Object findDetail(Role role) {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isBlank(role.getId())) {
            resultMap.put("result", "faild");
            resultMap.put("cause", "param error");
        } else {
            resultMap.put("result", "success");
            resultMap.put("data", roleService.findDetail(role));
        }
        return resultMap;
    }
}
